package cn.aylog.test;

import cn.aylog.dao.UserDao;
import cn.aylog.pojo.User;
import com.mysql.cj.Session;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class Demo {
    SqlSession session = null;

    @Before
    public void before() {
        String path = "mybatis.config.xml";
        try(InputStream is = Resources.getResourceAsStream(path)) {
            SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is);
            session = ssf.openSession();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @After
    public void after() {
        session.close();
    }

    @Test
    public void run1() throws Exception {
//        String path = "mybatis.config.xml";
//        InputStream is = Resources.getResourceAsStream(path);
//        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is);
//        SqlSession session = ssf.openSession();
        UserDao mapper = session.getMapper(UserDao.class);
        List<User> users = mapper.selectAll();
        System.out.println(users);
    }



}
